package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("AddressController")
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址管理相关接口")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result<List<AddressBook>> list(){
        log.info("查询当前登录用户的所有地址信息");
        List<AddressBook>list = addressService.list();
        return Result.success(list);
    }

    /**
     * 设置地址为默认值
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefaultAddressBook(@RequestBody AddressBook addressBook){
        log.info("设置默认地址:{}",addressBook);
        addressService.setDefaultAddressBook(addressBook);
        return Result.success();
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("修改地址")
    public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址:{}",addressBook);
        addressService.update(addressBook);
        return Result.success();
    }


    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id){
        log.info("根据id查询地址:{}",id);
        AddressBook addressBook = addressService.getAddressBookById(id);
        return Result.success(addressBook);
    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增地址:{}",addressBook);
        addressService.save(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result delete(Integer id){
        log.info("根据id删除地址:{}",id);
        addressService.delete(id);
        return Result.success();
    }
}
